package com.tiancheng.trade.order.channel.apple;

/**
 * 苹果支付渠道
 * @Author: likailun
 * @Description:
 * @Date: create in 2022/6/17 2:41 下午
 */

import com.tiancheng.trade.order.channel.AbstractPayChannel;
import com.tiancheng.trade.commom.web.model.request.RefundDTO;
import com.tiancheng.trade.order.model.Order;
import com.tiancheng.trade.order.model.RefundOrders;
import com.tiancheng.trade.order.model.RefundOrdersDetail;
import com.tiancheng.trade.order.vo.open.order.OpenRefundVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
public class ApplePayChannel extends AbstractPayChannel {
    @Override
    public RefundOrders generateRefunds(OpenRefundVO refundVO, Order order) {
        return null;
    }

    @Override
    public RefundDTO createAndSubmit(OpenRefundVO refundVO, RefundOrders refundOrders) {
        final List<RefundOrdersDetail> refundOrdersDetails = refundOrders.getSubRefundOrders();
        RefundDTO refundDTO = new RefundDTO();
        refundDTO.setUid(refundVO.getUid());
        refundDTO.setPaymentId(refundOrdersDetails.get(0).getPaymentId());
        refundDTO.setPayFlowId(refundOrdersDetails.get(0).getPayFlowId());
        refundDTO.setAmount(refundOrdersDetails.stream().mapToInt(RefundOrdersDetail::getRefundApplyAmount).sum());

        refundDTO.setOrderAmount(refundOrdersDetails.stream().mapToInt(RefundOrdersDetail::getSubOrderAmount).sum());
        refundDTO.setDiscount(refundOrdersDetails.stream().mapToInt(RefundOrdersDetail::getPlatformDiscount).sum());
        refundDTO.setSubOrders(new ArrayList<>());

        refundOrdersDetails.forEach(refund -> {
            RefundDTO.SubOrderRefundDTO subOrderRefundDTO = new RefundDTO.SubOrderRefundDTO();
            subOrderRefundDTO.setSubOrderId(refund.getSubOrderId());
            subOrderRefundDTO.setSubPaymentId(refund.getSubPaymentId());
            subOrderRefundDTO.setAmount(refund.getRefundApplyAmount());
            subOrderRefundDTO.setOrderAmount(refund.getSubOrderAmount());
            subOrderRefundDTO.setDiscount(refund.getPlatformDiscount());
            refundDTO.getSubOrders().add(subOrderRefundDTO);
        });
        return refundDTO;
    }
}
